package com.salt.modules.sys.web;

import com.salt.common.json.AjaxJson;
import com.salt.common.utils.StringUtils;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.Dock;
import com.salt.modules.sys.entity.Menu;
import com.salt.modules.sys.entity.User;
import com.salt.modules.sys.service.DockService;
import com.salt.modules.sys.utils.UserUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author salt
 * @Date 2021/12/23
 * @Version 1.0.1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/dock")
public class DockController extends BaseController {

    private final DockService dockService;

    @PostMapping("save")
    public AjaxJson save(Dock dock) {
        List<Dock> list = dockService.findList(dock);
        if (list != null && !list.isEmpty())
            return AjaxJson.error("该菜单已添加，不能重复添加!");
        User user = UserUtils.getUser();
        if (null == user)
            return AjaxJson.error("请登录后在添加!");
        dock.setUserId(user.getId());
        dockService.save(dock);
        return AjaxJson.success("添加成功!");
    }

    @GetMapping("list")
    public AjaxJson list(Dock dock) {
        dock.setUserId(UserUtils.getUser().getId());
        List<Menu> menuList = dockService.findByDock(dock);
        return AjaxJson.success().put("menuList", menuList);
    }

    @DeleteMapping("delete")
    public AjaxJson delete(Dock dock) {
        if (StringUtils.isNotBlank(dock.getId())) {
            dockService.delete(dock);
            return AjaxJson.success("删除成功!");
        }
        return AjaxJson.error("删除失败!");
    }

}
